import datetime
import time
from xmlrpc.client import DateTime

from django.shortcuts import render

# Create your views here.
from django.views import View

from employee.models import *

class ChenkView(View):
    def get(self,request):
        return render(request,'admin-check.html')

class ServeyView(View):
    def get(self,request):
        ments = Ment.objects.all()
        employees = Employee.objects.all()
        return render(request,'ac-servey.html',{'ments':ments,'employees':employees})
    def post(self,request):
        ments = Ment.objects.all()
        ss = request.POST.get('mmm','0')
        print(ss)
        if ss =='0':
            employees = Employee.objects.all()
            return render(request, 'ac-servey.html', {'ments': ments, 'employees': employees})
        else:
            ment = Ment.objects.get(ment_name=ss)
            employees = ment.m.all()
            return render(request, 'ac-servey.html', {'ments': ments, 'employees': employees})
class SignView(View):
    def get(self,request):
        employees = Employee.objects.all()
        return render(request,'ac-sign.html',{'employees':employees})

class ManageView(View):
    def get(self,request):
        return render(request,'ac-manage.html')

class SigninTimeView(View):
    def post(self,request):
        user_id = request.session.get('s_id')
        emp = Employee.objects.get(s_id=user_id)
        ti = time.strftime(("%H:%M"))
        now = datetime.datetime.now()
        if request.session.get('signintime',None):
            del request.session['signintime']
            request.session['signintime'] = ti
            start_date = datetime.date(now.year,now.month,now.day)
            end_date = datetime.date(now.year,now.month,now.day+1)
            signintime.objects.filter(time__range=(start_date, end_date)).delete()
            sign = signintime(emp=emp,time=now)
            sign.save()
            return render(request, 'admin-check.html')
        request.session['signintime']= ti
        sign = signintime(emp=emp,time=now)
        sign.save()
        return render(request, 'admin-check.html')

class SignoutTimeView(View):
    def post(self,request):
        user_id = request.session.get('s_id')
        emp = Employee.objects.get(s_id=user_id)
        ti = time.strftime(("%H:%M"))
        now = datetime.datetime.now()
        if request.session.get('signouttime',None):
            del request.session['signouttime']
            request.session['signouttime'] = ti
            start_date = datetime.date(now.year,now.month,now.day)
            end_date = datetime.date(now.year,now.month,now.day+1)
            signouttime.objects.filter(time__range=(start_date, end_date)).delete()
            sign = signouttime(emp=emp,time=now)
            sign.save()
            return render(request, 'admin-check.html')
        request.session['signouttime']= ti
        sign = signouttime(emp=emp,time=now)
        sign.save()
        return render(request, 'admin-check.html')